package com.resolution.dietaryassessment.view.mydietary;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import android.widget.Button;

import com.resolution.dietaryassessment.BaseActivity;
import com.resolution.dietaryassessment.R;
import com.resolution.dietaryassessment.listener.MyListener;
import com.resolution.dietaryassessment.view.mydietary.fragment.ChoiceFoodFrag;
import com.resolution.dietaryassessment.view.mydietary.fragment.DailyNutritionFrag;

import es.dmoral.toasty.Toasty;

public class FragHolderActivity extends BaseActivity{

    private static final String TAG = "aaa";
    private int mQuantity,protein,fat,carbon,mQuanKJ;
    private int state = 1;
    private ChoiceFoodFrag choiceFoodFrag = new ChoiceFoodFrag();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frag_holder);
        initView();
    }

    @SuppressLint("CheckResult")
    public void initView() {
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("info");
        mQuantity = bundle.getInt("热量", 0);
        protein = bundle.getInt("蛋白质", 0);
        fat = bundle.getInt("脂肪", 0);
        carbon = bundle.getInt("碳水", 0);
        mQuanKJ = (int)(mQuantity * 4.1858518);

        Log.d(TAG, "initView: " + protein);

        Button next,reMain,leave;
        next = findViewById(R.id.next_btn);
        reMain = findViewById(R.id.re_fanhui_btn);
        leave = findViewById(R.id.leave_btn);

        leave.setOnClickListener(new MyListener(this));
        reMain.setOnClickListener(v -> onBackPressed());
        next.setOnClickListener(v -> {
            if (state == 1){
                addChoiceFoodFrag();
            }else if (choiceFoodFrag.inspectObj()){
                start(FoodResultActivity.class);
            }else {
                Toasty.error(FragHolderActivity.this,"请至少添加一项食物").show();
            }
        });
        addDailyNutritionFrag();
    }

    /**
     * 每日营养所需frag
     */
    private void addDailyNutritionFrag() {
        DailyNutritionFrag dailyNutritionFrag = new DailyNutritionFrag();
        Bundle bundle = new Bundle();
        bundle.putInt("热量",mQuantity);
        bundle.putInt("蛋白质", protein);
        bundle.putInt("脂肪", fat);
        bundle.putInt("碳水", carbon);
        bundle.putInt("热量2", mQuanKJ);
        dailyNutritionFrag.setArguments(bundle);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.frag_group,dailyNutritionFrag)
                .commit();
    }

    /**
     * 食物选择frag
     */
    private void addChoiceFoodFrag(){
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.frag_group,choiceFoodFrag)
                .commit();
        state = 2;
    }
}
